package org.example.smartEducationBackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.example.smartEducationBackend.entity.POJO.Resource;
import org.example.smartEducationBackend.entity.POJO.RestResult;
import org.example.smartEducationBackend.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/resources")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/pageRequest")
    public RestResult<Map<String, Object>> getResources(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "12") int pageSize,
            @RequestParam(defaultValue = "2") String type,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(required = false) String labels,
            @RequestParam(required = false) String search) {

        // 构建分页对象
        Page<Resource> pageRequest = new Page<>(page, pageSize);

        // 调用服务层获取数据
        return RestResult.ok(resourceService.getResources(pageRequest, type, difficulty, labels != null ? List.of(labels.split(",")) : null, search));
    }

    @GetMapping("/getResource")
    public RestResult<Resource> deleteResource(@RequestParam Integer id) {
        Resource resource = resourceService.getById(id);
        return RestResult.ok(resource);
    }

    @GetMapping("/getCFResource")
    public RestResult<List<Resource>> getCFResource(@RequestParam Integer userId) {
        List<Resource> resourceList = resourceService.getCFResource(userId);
        return RestResult.ok(resourceList);
    }
}